<?php

require_once( '../system/smarty.inc' );

require_once( '../system/mdao/Class_M_ELECTRIC.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_COST,$login['staff_auth']);

$dosearch = FALSE ;
$dao = new Class_M_ELECTRIC;

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	 $forms =  $_SESSION['MSEARCH']['COST_FLIER'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['COST_FLIER'] = $forms;
}

$wherearr =  array();
/*
//---CSVダウンロード　処理開始

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "areaad".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$data = $dao->searchcsv( $wherearr, $shop_name );
	$colums = array();
	$col_heads = array(
		'matrix_cd'        => array( 'マトリックスID',  1),
		'year4'            => array( '年度',            1),
		'week_cd'          => array( '週',              1),
		'flier_date'       => array( '折込日',          1),
		'area_cd'          => array( 'エリアコード',    1),
		'area_name'        => array( 'エリア名',        1),
		'publish_area_name'=> array( '配布エリア名',    1),
		'area_nums'        => array( 'エリア部数',      1),
		'hand_name'        => array( '取扱業者',        1),
		'media_name'       => array( '媒体名',          1),
		'memo'             => array( '備考',            1),
		'shop_cd'          => array( '店舗コード',      1),
		'shop_name'        => array( '店舗名',          1),
		'shop_per'         => array( '店舗部数%',       1),
		'shop_nums'        => array( '計算部数',        1),
		'u_time'           => array( '更新日時',        3),
		'c_time'           => array( '登録日時',        3),
		'm_user_name'      => array( '更新者',          1)
	);

	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	//データ整形開始
	foreach ($data as $k => $v) {
		$data[$k]["flier_date"]  =  Class_PWD::int8_to_date($v["flier_date"]);
		$data[$k]["c_time"]      =  Class_PWD::dateTime_to_YmdHi($v["c_time"]);
		$data[$k]["u_time"]      =  Class_PWD::dateTime_to_YmdHi($v["u_time"]);
	}
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;
}
/*

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "downloadd" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "areaad_detail".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$data = $dao->searchcsv( $wherearr, $shop_name ,'d');
	$colums = array();

	$col_heads = array(
		'area_cd'          => array( 'エリアコード',    1),
		'shop_cd'          => array( '店舗コード',      1),
		'shop_name'        => array( '店舗名',          1),
		'shop_per'         => array( '店舗部数%',       1),
		'shop_nums'        => array( '計算部数',        1),
		'u_time'           => array( '更新日時',        3),
		'c_time'           => array( '登録日時',        3),
		'm_user_name'      => array( '更新者',          1)
	);

	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	//データ整形開始
	foreach ($data as $k => $v) {
		$data[$k]["flier_date"]  =  Class_PWD::int8_to_date($v["flier_date"]);
		$data[$k]["c_time"]      =  Class_PWD::dateTime_to_YmdHi($v["c_time"]);
		$data[$k]["u_time"]      =  Class_PWD::dateTime_to_YmdHi($v["u_time"]);
	}
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;
}

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "downloadb" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "areaad_base".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$data = $dao->searchcsv( $wherearr, $shop_name,'b' );
	$colums = array();
	$col_heads = array(
		'area_cd'          => array( 'エリアコード',    1),
		'area_name'        => array( 'エリア名',        1),
		'publish_area_name'=> array( '配布エリア名',    1),
		'area_nums'        => array( 'エリア部数',      1),
		'hand_name'        => array( '取扱業者',        1),
		'media_name'       => array( '媒体名',          1),
		'matrix_cd'        => array( 'マトリックスID',  1),
		'memo'             => array( '備考',            1)
	);

	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	//データ整形開始
	foreach ($data as $k => $v) {
		//$data[$k]["flier_date"]   =  Class_PWD::int8_to_date($v["flier_date"]);
		//$data[$k]["c_time"]      =  Class_PWD::dateTime_to_YmdHi($v["c_time"]);
		//$data[$k]["u_time"]      =  Class_PWD::dateTime_to_YmdHi($v["u_time"]);
	}
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;
}
//---CSVダウンロード　処理完了
 * */
$dosearch = TRUE;
if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	$dosearch = TRUE;
}

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];
}else{
	$page['current'] = 1;
}

$page['item'] = PAGE_MAX;

if ($dosearch) {
	//$page内容は関数内部更新されます。
	if ($login['staff_auth'] == "G-9" ) {
		$data = $dao->search( $wherearr, $page, $login['autoid']);
		
	}else {
		$data = $dao->search( $wherearr, $page );
	}
	//表示ために整形開始
	foreach ($data as $k => $v ) {
		$data[$k]["shops"] = $dao->getShopCount( $v["id"] ); 
	}

}
//表示ために整形完了



/*
$electrics = Class_M_OTHER::get_electric_array(PLEASE_SELECT_ARR);
$smarty->assign('electrics', $electrics );
*/
if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data', $data);
//if( isset($disp) )  $smarty->assign('disp', $disp);
if( isset($page) )  $smarty->assign('page', $page);

$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('mcost_tvsp_electric.html');
?>